Python Project 7: Read & Write JSON Files

 ✅ Python Project 7: Read & Write JSON Files 🔄📄

📌 Task:
Write a Python program that reads a JSON file, makes some changes, and then saves it into a new JSON file.

🧠 What You’ll Learn:

  • How to use Python’s json module

  • How to read & write JSON files

  • How to work with nested JSON data

✅ Python Code Example

                            import json

# Step 1: Read JSON file

with open("data.json", "r") as f:

    data = json.load(f)

    print("Original Data:")

    print(data)


# Step 2: Modify data (example: add a new key-value pair)

data["status"] = "processed"


# Step 3: Save updated data into a new JSON file

with open("updated_data.json", "w") as f:

    json.dump(data, f, indent=4)


print("\nUpdated JSON saved successfully!")


📥 How It Works:

  1. Reads data.json file

  2. Adds/updates data (e.g., status = "processed")

  3. Saves changes into updated_data.json


🔧 Try Yourself:

  • Loop through a list in JSON

  • Extract values from nested data

  • Combine multiple JSON files into one


💡 Where You Can Use It:

  • Saving API responses

  • Updating config files

  • Storing user data in apps



    

Comments

Popular posts from this blog

🔁 Top 30 Python List Methods You Should Know

✅ Python – Quick Start Guide for Beginners